package com.bootdo.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.VeriCode;
import com.bootdo.system.service.VeriCodeService;
import com.bootdo.system.util.SmsUtil;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/sys/veriCode")
@Controller
@Api(value = "/sys/veriCode", description = "短信验证码操作接口")
public class VeriCodeController extends BaseController {
	@Autowired
	VeriCodeService service;


	@ApiOperation(value="短信验证码查询", notes="根据条件查询短信验证码", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "短信验证码id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "phone", value = "手机号码", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "code", value = "验证码", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,paramType="query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true,paramType="query", dataType = "Integer")
	})
	@GetMapping("/list")
	@ResponseBody
	R list(@RequestParam Map<String,Object> param)  throws Exception{
		 Query query = new Query(param);
		List<VeriCode> VeriCode = service.list(query);
		PageInfo<VeriCode> pageInfo = new PageInfo<VeriCode>(VeriCode);
		int total = (int)pageInfo.getTotal();
		PageUtils pageUtil = new PageUtils(VeriCode, total);
		return new R("list",pageUtil);
	}

	@ApiOperation(value="短信验证码详情", notes="根据条件查询短信验证码", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "短信验证码id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/edit")
	@ResponseBody
	R edit(Long id)  throws Exception{
		return new R("obj",service.get(id));
	}

	@ApiOperation(value="短信验证码保存", notes="保存短信验证码", response=String.class)
	@PostMapping("/save")
	@ResponseBody
	R save(@RequestBody VeriCode code)  throws Exception{
		code.setCreateTime(new Date());
		if (service.save(code) > 0) {
			return R.saveOk(code.getId().toString());
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="短信验证码更新", notes="更新短信验证码", response=String.class)
	@PostMapping("/update")
	@ResponseBody
	R update(@RequestBody VeriCode baby)  throws Exception{
		if (service.update(baby) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@ApiOperation(value="短信验证码删除", notes="根据短信验证码id删除", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "短信验证码id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/remove")
	@ResponseBody
	R remove(Long id)  throws Exception{
		if (service.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}
	
	@ApiOperation(value="短信发送", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true,paramType="query", dataType = "String"),
	})
	@GetMapping("/sendSms")
	@ResponseBody
	R sendSms(String phone)  throws Exception{
		service.removeByPhone(phone);
		String code = (int)((Math.random()*9+1)*100000)+"";
		SmsUtil.sendSms(phone, code);
		VeriCode vc = new VeriCode();
		vc.setCode(code);
		vc.setPhone(phone);
		vc.setCodeType(1);
		vc.setCreateTime(new Date());
		if (service.save(vc) > 0) {
			return R.ok(vc.getId().toString());
		} else {
			return R.error(1, "发送失败");
		}
	}
}
